home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / bricon / source / file.c < prev    next >
Text File  |  1991-10-18  |  6KB  |  284 lines

  1. #define    TRUE    1
  2. #define    FALSE    0
  3. #define    ERR    (-1)
  4.  
  5. #define BUFLEN  128
  6.  
  7.     int         fext_top=0;
  8.     int         fext_pos=0;
  9.     char far    *dta_bak;
  10.     char         path_top[BUFLEN];
  11.     struct {
  12.         unsigned char    dd_dmy;
  13.         unsigned char    dd_drv;
  14.         unsigned char    dd_path[8];
  15.         unsigned char    dd_extn[3];
  16.         unsigned char    dd_resv[8];
  17.         unsigned char    dd_att;
  18.         unsigned short    dd_time,dd_date;
  19.         unsigned long    dd_size;
  20.         char        dd_name[14];
  21.     } dta;
  22.  
  23. static    int    fext_chk=FALSE;
  24. static  char    exec_chk=FALSE;
  25. static  char    path_chk=FALSE;
  26. static  char    *path_pos;
  27. static  char    *wild_pos;
  28. static  char    wild_buf[BUFLEN];
  29. static  char    path_buf[BUFLEN];
  30.  
  31. /******** Compile to HIS.ASM ***********
  32.  
  33. static    char    tolow(char ch)
  34. {
  35.     if ( ch >= 'A' && ch <= 'Z' )
  36.         return (ch | 0x20);
  37.     else
  38.         return ch;
  39. }
  40. static    void    strlow(register char *str)
  41. {
  42.     while ( *str != '\0' ) {
  43.         *str = tolow(*str);
  44.         str++;
  45.     }
  46. }
  47. static    void    DTA_init()
  48. {
  49.     union REGS regs;
  50.     struct SREGS seg;
  51.     char far *p;
  52.  
  53.     regs.h.ah = 0x2F;
  54.     intdosx(®s,®s,&seg);
  55.     FP_SEG(dta_bak) = seg.es;
  56.     FP_OFF(dta_bak) = regs.x.bx;
  57.  
  58.     p = (char far *)&dta;
  59.     regs.h.ah = 0x1a;
  60.     seg.ds = FP_SEG(p);
  61.     regs.x.dx = FP_OFF(p);
  62.     intdosx(®s,®s,&seg);
  63. }
  64. static    void    DTA_end()
  65. {
  66.     union REGS regs;
  67.     struct SREGS seg;
  68.  
  69.     regs.h.ah = 0x1a;
  70.     seg.ds = FP_SEG(dta_bak);
  71.     regs.x.dx = FP_OFF(dta_bak);
  72.     intdosx(®s,®s,&seg);
  73. }
  74. static    int    farst_call(char *wild)
  75. {
  76.     union REGS regs;
  77.     struct SREGS seg;
  78.     char far *p;
  79.  
  80.     p = wild;
  81.     regs.h.ah = 0x4e;
  82.     regs.x.cx = 0x21;
  83.     seg.ds = FP_SEG(p);
  84.     regs.x.dx = FP_OFF(p);
  85.     intdosx(®s,®s,&seg);
  86.     return (regs.x.cflag == 0 ? TRUE:FALSE);
  87. }
  88. static    int    next_call(void)
  89. {
  90.     union REGS regs;
  91.  
  92.     regs.h.ah = 0x4f;
  93.     intdos(®s,®s);
  94.     return (regs.x.cflag == 0 ? TRUE:FALSE);
  95. }
  96. ******** End of Compile ***********/
  97.  
  98. static    int     path_set(void)
  99. {
  100.     char    *p,*s;
  101.  
  102.     if ( *path_pos == '\0' )
  103.         return FALSE;
  104.  
  105.     p = wild_buf;
  106.     while ( *path_pos != '\0' && p < &(wild_buf[BUFLEN-1]) ) {
  107.         if ( *path_pos == ';' ) {
  108.             path_pos++;
  109.             break;
  110.         } else
  111.             *(p++) = *(path_pos++);
  112.     }
  113.  
  114.     if ( p > wild_buf && *(p-1) != '\\' && p < &(wild_buf[BUFLEN-1]) )
  115.         *(p++) = '\\';
  116.  
  117.     s = path_buf;
  118.     while ( *s != '\0' && p < &(wild_buf[BUFLEN-1]) )
  119.         *(p++) = *(s++);
  120.     *p = '\0';
  121.  
  122.     return TRUE;
  123. }
  124. static    int     file_cmp(void)
  125. {
  126.     char *p;
  127.  
  128.     p = dta.dd_name;
  129.     while ( *p != '.' && *p != '\0' )
  130.     p++;
  131.  
  132.     if ( exec_chk ) {
  133.         if ( strcmp(p,".BAT") == 0 ||
  134.              strcmp(p,".COM") == 0 ||
  135.              strcmp(p,".EXE") == 0 )
  136.             return TRUE;
  137.         else
  138.             return FALSE;
  139.     } else
  140.         return TRUE;
  141. }
  142. static    int     farst_dir(void)
  143. {
  144.     return farst_call(wild_buf);
  145. }
  146. static    int     next_dir(void)
  147. {
  148.     do {
  149.         while ( !next_call() ) {
  150.             do {
  151.                 if ( !path_chk   || !path_set() )
  152.                     return FALSE;
  153.             } while ( !farst_dir() );
  154.             break;
  155.         }
  156.     } while ( !file_cmp() );
  157.  
  158.     return TRUE;
  159. }
  160. static    int     wild_set(char *str,int len)
  161. {
  162.     int     n;
  163.     char    *p;
  164.  
  165.     n = len;
  166.     while ( n > 0 ) {
  167.         n--;
  168.         if ( str[n] == ' ' || str[n] == '\t' ) {
  169.             n++;
  170.             break;
  171.         }
  172.     }
  173.  
  174.     exec_chk = path_chk = (n > 0 ? FALSE:TRUE);
  175.  
  176.     fext_top = fext_pos = n;
  177.     p = wild_pos = wild_buf;
  178.     while ( n < len && p < &(wild_buf[BUFLEN-1]) ) {
  179.         if ( str[n] == ':' || str[n] == '\\' ) {
  180.             fext_pos = n + 1;
  181.             wild_pos = p + 1;
  182.             path_chk = FALSE;
  183.         }
  184.         *(p++) = str[n++];
  185.     }
  186.     *p = '\0';
  187.  
  188.     n = 0;
  189.     p = wild_pos;
  190.     while ( *p != '\0' ) {
  191.         if ( *p == '.' )
  192.             n |= 2;
  193.         else if ( *p == '*' || *p == '?' )
  194.             n |= 1;
  195.         p++;
  196.     }
  197.  
  198.     switch(n) {
  199.     case 0: strcpy(p,"*.*"); break;
  200.     case 1: strcpy(p,".*"); break;
  201.     case 2: case 3: exec_chk = FALSE; break;
  202.     }
  203.  
  204.     if ( path_chk ) {
  205.         strcpy(path_buf,wild_buf);
  206.     wild_pos = path_buf + ((int)wild_pos - (int)wild_buf);
  207.         path_pos = path_top;
  208.     }
  209.  
  210.     if ( farst_dir() && file_cmp() )
  211.         return TRUE;
  212.     else
  213.         return next_dir();
  214. }
  215.  
  216. char    *file_ext(char *str,int pos)
  217. {
  218.     int    rc;
  219.     char   *p;
  220.  
  221.     if ( fext_chk == FALSE ) {
  222.         DTA_init();
  223.         rc = wild_set(str,pos);
  224.         fext_chk = TRUE;
  225.     } else
  226.         rc = next_dir();
  227.  
  228.     if ( rc == FALSE ) {
  229.         DTA_end();
  230.         fext_chk = FALSE;
  231.     BEEP();
  232.         return wild_pos;
  233.     }
  234.  
  235.     strlow(dta.dd_name);
  236.  
  237.     if ( exec_chk ) {
  238.     p = dta.dd_name;
  239.     while ( *p != '.' && *p != '\0' )
  240.         p++;
  241.     *p = '\0';
  242.     }
  243.  
  244.     return dta.dd_name;
  245. }
  246. void    file_end(void)
  247. {
  248.     if ( fext_chk != FALSE ) {
  249.     while ( next_call() );
  250.         DTA_end();
  251.         fext_chk = FALSE;
  252.     }
  253. }
  254. /******** Compile to HIS.ASM ***********
  255. static    int     envcmp(char far *p,char *s)
  256. {
  257.     while ( *s != '\0' ) {
  258.        if ( tolow(*p) != *s )
  259.             return FALSE;
  260.     p++;
  261.     s++; 
  262.     }
  263.     return TRUE;
  264. }
  265. void    Path_init(void)
  266. {
  267.     short far *s;
  268.  
  269.     FP_SEG(s) = _psp;
  270.     FP_OFF(s) = 0x2C;
  271.  
  272.     FP_SEG(path_top) = *s;
  273.     FP_OFF(path_top) = 0;
  274.  
  275.     while ( *path_top != '\0' ) {
  276.     if ( envcmp(path_top,"path=") ) {
  277.         path_top += 5;
  278.         break;
  279.     }
  280.         while ( *(path_top++) != '\0' );
  281.     }
  282. }
  283. ******** End of Compile ***********/
  284.